#!/bin/bash

Check_Stack_Choose()
{
    Check_Stack
    if [[ "${Get_Stack}" = "lnmp" && "${Stack}" = "" ]]; then
        echo "Current Stack: ${Get_Stack},please run: ./upgrade.sh php"
        sleep 3
        exit 1
     elif [[ "${Get_Stack}" = "lnmpa" || "${Get_Stack}" = "lamp" ]] && [[ "${Stack}" = "lnmp" ]]; then
        echo "Current Stack: ${Get_Stack},please run: ./upgrade.sh phpa"
        sleep 3
        exit 1
    fi
}

Start_Upgrade_PHP()
{
    Check_Stack_Choose
    Check_DB
    php_version=""
    Get_PHP_Ext_Dir
    echo "Current PHP Version:${Cur_PHP_Version}"
    echo "You can get version number from http://www.php.net/"
    read -p "Please enter a PHP Version you want: " php_version
    if [ "${php_version}" = "" ]; then
        echo "Error: You must enter a corrent php version!!"
        exit 1
    fi

    Press_Start

    cd ${cur_dir}/src
    if [ -s php-${php_version}.tar.gz ]; then
        echo "php-${php_version}.tar.gz [found]"
     else
        echo "Error: php-$php_version.tar.gz not found!!!download now..."
        cd ${cur_dir}/src
        wget -c ${Download_Mirror}/php/php-${php_version}.tar.gz
        if [ $? -eq 0 ]; then
            echo "Download php-${php_version}.tar.gz successfully!"
         else
            wget -c http://www.php.net/distributions/php-${php_version}.tar.gz
            if [ $? -eq 0 ]; then
                echo "Download php-${php_version}.tar.gz successfully!"
             else
                wget -c http://museum.php.net/php5/php-${php_version}.tar.gz
                if [ $? -eq 0 ]; then
                    echo "Download php-${php_version}.tar.gz successfully!"
                 else
                    echo "You enter PHP Version was:"${php_version}
                    Echo_Red "Error! You entered a wrong version number, please check!"
                    sleep 5
                    exit 1
                fi
            fi
        fi
    fi

    if echo "${php_version}" | grep -Eqi '^5.2.';then
        Download_Files ${Download_Mirror}/phpfpm/php-${php_version}-fpm-0.5.14.diff.gz php-${php_version}-fpm-0.5.14.diff.gz
    fi
    lnmp stop

    if [ "${Stack}" = "lnmp" ]; then
        if [ -f /usr/local/php-${Cur_PHP_Version}/php-fpm-${Cur_PHP_Version} ]; then
            rm -f /etc/init.d/php-fpm
         else
            mv /etc/init.d/php-fpm /usr/local/php-${Cur_PHP_Version}/php-fpm-${Cur_PHP_Version}
        fi
     else
        if echo "${Cur_PHP_Version}" | grep -Eqi '^7.';then
            mv /usr/local/apache/modules/libphp7.so /usr/local/apache/modules/libphp7.so-${Cur_PHP_Version}
         else
            mv /usr/local/apache/modules/libphp5.so /usr/local/apache/modules/libphp5.so-${Cur_PHP_Version}
        fi
    fi
    rm -f /usr/local/php
}

Check_PHP53_Curl()
{
    if [ "${DISTRO}" = "Fedora" ];then
        PHP53_With_Curl='y'
     elif echo "${Ubuntu_Version}" | grep -Eqi '^14.1';then
        PHP53_With_Curl='y'
     elif echo "${Ubuntu_Version}" | grep -Eqi '^15.';then 
        PHP53_With_Curl='y'
     elif echo "${Debian_Version}" | grep -Eqi '^8.';then
        PHP53_With_Curl='y'
    fi
}

Ln_PHP_Bin()
{
    ln -sf /usr/local/php/bin/php /usr/bin/php
    ln -sf /usr/local/php/bin/phpize /usr/bin/phpize
    ln -sf /usr/local/php/bin/pear /usr/bin/pear
    ln -sf /usr/local/php/bin/pecl /usr/bin/pecl
    if [ "${Stack}" = "lnmp" ]; then
        ln -sf /usr/local/php/sbin/php-fpm /usr/bin/php-fpm
    fi
}

Pear_Pecl_Set()
{
    pear config-set php_ini /usr/local/php/etc/php.ini
    pecl config-set php_ini /usr/local/php/etc/php.ini
}

Check_PHP_Upgrade_Files()
{
    if [ "${Stack}" = "lnmp" ]; then
        if [[ -s /usr/local/php/sbin/php-fpm && -s /etc/init.d/php-fpm && -s /usr/local/php/etc/php.ini && -s /usr/local/php/bin/php ]]; then
            Echo_Green "======== upgrade php completed ======"
         else
            Echo_Red "======== upgrade php failed ======"
            Echo_Red "upgrade php log: /root/upgrade_lnmp_php.log"
            echo "You mail upgrade_lnmp_php.log to hongm@sina.com for help."
        fi
     else
        if [[ (-s /usr/local/apache/modules/libphp5.so || -s /usr/local/apache/modules/libphp7.so) && -s /usr/local/php/etc/php.ini && -s /usr/local/php/bin/php ]]; then
            Echo_Green "======== upgrade php completed ======"
         else
            Echo_Red "======== upgrade php failed ======"
            Echo_Red "upgrade php log: /root/upgrade_a_php.log"
            echo "You mail upgrade_a_php.log to hongm@sina.com for help."
        fi
    fi
}

Upgrade_PHP_52()
{
    Check_Curl
    Check_Autoconf
    cd ${cur_dir}/src && rm -rf php-${php_version}
    tar zxf php-${php_version}.tar.gz
    if [ "${Stack}" = "lnmp" ]; then
        gzip -cd php-${php_version}-fpm-0.5.14.diff.gz | patch -d php-${php_version} -p1
    fi
    cd php-${php_version}/
    patch -p1 < ${cur_dir}/src/patch/php-5.2.17-max-input-vars.patch
    patch -p0 < ${cur_dir}/src/patch/php-5.2.17-xml.patch
    patch -p1 < ${cur_dir}/src/patch/debian_patches_disable_SSLv2_for_openssl_1_0_0.patch
    patch -p1 < ${cur_dir}/src/patch/php-5.2-multipart-form-data.patch
    ./buildconf --force
    if [ "${Stack}" = "lnmp" ]; then
        ./configure --prefix=/usr/local/php-${php_version} --with-config-file-path=/usr/local/php/etc --with-mysql=${MySQL_Dir} --with-mysqli=${MySQL_Config} --with-pdo-mysql=${MySQL_Dir} --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-discard-path --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl=/usr/local/curl --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --with-mime-magic ${PHP_Modules_Arguments}
     else
        ./configure --prefix=/usr/local/php-${php_version} --with-config-file-path=/usr/local/php/etc --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=${MySQL_Dir} --with-mysqli=${MySQL_Config} --with-pdo-mysql=${MySQL_Dir} --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-discard-path --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl=/usr/local/curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --with-mime-magic ${PHP_Modules_Arguments}
        sed -i 's/#LoadModule php5_module/LoadModule php5_module/g' /usr/local/apache/conf/httpd.conf
    fi
    make ZEND_EXTRA_LIBS='-liconv'
    make install
    rm -f /usr/local/php
    ln -sf /usr/local/php-${php_version} /usr/local/php
    mkdir -p /usr/local/php/etc
    \cp php.ini-dist /usr/local/php/etc/php.ini
    cd ../
    Ln_PHP_Bin
    # php extensions
    sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"\n#' /usr/local/php/etc/php.ini
    sed -i 's#output_buffering = Off#output_buffering = On#' /usr/local/php/etc/php.ini
    sed -i 's/post_max_size = 8M/post_max_size = 512M/g' /usr/local/php/etc/php.ini
    sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 500M/g' /usr/local/php/etc/php.ini
    sed -i 's/;date.timezone =/date.timezone = PRC/g' /usr/local/php/etc/php.ini
    sed -i 's/short_open_tag = Off/short_open_tag = On/g' /usr/local/php/etc/php.ini
    sed -i 's/; cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini
    sed -i 's/max_execution_time = 30/max_execution_time = 300/g' /usr/local/php/etc/php.ini
    sed -i 's/max_input_time = 60/max_input_time = 600/g' /usr/local/php/etc/php.ini
    sed -i 's/memory_limit = 128M/memory_limit = 2048M/g' /usr/local/php/etc/php.ini
    sed -i 's/disable_functions =.*/disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,fsocket/g' /usr/local/php/etc/php.ini
    Pear_Pecl_Set
    cd ${cur_dir}/src
    if [ "${Is_64bit}" = "y" ] ; then
        Download_Files ${Download_Mirror}/zend/ZendOptimizer-3.3.9-linux-glibc23-x86_64.tar.gz ZendOptimizer-3.3.9-linux-glibc23-x86_64.tar.gz
        tar zxf ZendOptimizer-3.3.9-linux-glibc23-x86_64.tar.gz
        mkdir -p /usr/local/zend/
        \cp ZendOptimizer-3.3.9-linux-glibc23-x86_64/data/5_2_x_comp/ZendOptimizer.so /usr/local/zend/
     else
        Download_Files ${Download_Mirror}/zend/ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz
        tar zxf ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz
        mkdir -p /usr/local/zend/
        \cp ZendOptimizer-3.3.9-linux-glibc23-i386/data/5_2_x_comp/ZendOptimizer.so /usr/local/zend/
    fi
    cat >>/usr/local/php/etc/php.ini<<EOF
;eaccelerator
;ionCube
[Zend Optimizer] 
zend_optimizer.optimization_level=1 
zend_extension="/usr/local/zend/ZendOptimizer.so"
;xcache
;xcache end
EOF
    if [ "${Stack}" = "lnmp" ]; then
        rm -f /usr/local/php/etc/php-fpm.conf
        \cp ${cur_dir}/conf/php-fpm5.2.conf /usr/local/php/etc/php-fpm.conf
        \cp ${cur_dir}/init.d/init.d.php-fpm5.2 /etc/init.d/php-fpm
        chmod +x /etc/init.d/php-fpm
    fi
    lnmp start
    Check_PHP_Upgrade_Files
}

Upgrade_PHP_53()
{
    Echo_Blue "Start install php-${php_version}"
    Tar_Cd php-${php_version}.tar.gz php-${php_version}
    Check_PHP53_Curl
    patch -p1 < ${cur_dir}/src/patch/php-5.3-multipart-form-data.patch
    if [ "${PHP53_With_Curl}" = "y" ]; then
        if [ "${Stack}" = "lnmp" ]; then
            ./configure --prefix=/usr/local/php-${php_version} --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl=/usr/local/curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo ${PHP_Modules_Arguments}
         else
            ./configure --prefix=/usr/local/php-${php_version} --with-config-file-path=/usr/local/php/etc --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl=/usr/local/curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo ${PHP_Modules_Arguments}
            sed -i 's/#LoadModule php5_module/LoadModule php5_module/g' /usr/local/apache/conf/httpd.conf
        fi
     else
        if [ "${Stack}" = "lnmp" ]; then
            ./configure --prefix=/usr/local/php-${php_version} --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo ${PHP_Modules_Arguments}
         else
            ./configure --prefix=/usr/local/php-${php_version} --with-config-file-path=/usr/local/php/etc --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo ${PHP_Modules_Arguments}
            sed -i 's/#LoadModule php5_module/LoadModule php5_module/g' /usr/local/apache/conf/httpd.conf
        fi
    fi
    make ZEND_EXTRA_LIBS='-liconv'
    make install
    rm -f /usr/local/php
    ln -sf /usr/local/php-${php_version} /usr/local/php
    Ln_PHP_Bin
    echo "Copy new php configure file."
    mkdir -p /usr/local/php/etc
    \cp php.ini-production /usr/local/php/etc/php.ini
    cd ${cur_dir}
    # php extensions
    echo "Modify php.ini......"
    sed -i 's/post_max_size = 8M/post_max_size = 512M/g' /usr/local/php/etc/php.ini
    sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 500M/g' /usr/local/php/etc/php.ini
    sed -i 's/;date.timezone =/date.timezone = PRC/g' /usr/local/php/etc/php.ini
    sed -i 's/short_open_tag = Off/short_open_tag = On/g' /usr/local/php/etc/php.ini
    sed -i 's/; cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini
    sed -i 's/;cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini
    sed -i 's/max_execution_time = 30/max_execution_time = 300/g' /usr/local/php/etc/php.ini
    sed -i 's/max_input_time = 60/max_input_time = 600/g' /usr/local/php/etc/php.ini
    sed -i 's/memory_limit = 128M/memory_limit = 2048M/g' /usr/local/php/etc/php.ini
    sed -i 's/disable_functions =.*/disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server/g' /usr/local/php/etc/php.ini
    Pear_Pecl_Set
    echo "Install ZendGuardLoader..."
    cd ${cur_dir}/src
    if [ "${Is_64bit}" = "y" ] ; then
        Download_Files ${Download_Mirror}/zend/ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz
        tar zxf ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz
        mkdir -p /usr/local/zend/
        \cp ZendGuardLoader-php-5.3-linux-glibc23-x86_64/php-5.3.x/ZendGuardLoader.so /usr/local/zend/
     else
        Download_Files ${Download_Mirror}/zend/ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz
        tar zxf ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz
        mkdir -p /usr/local/zend/
        \cp ZendGuardLoader-php-5.3-linux-glibc23-i386/php-5.3.x/ZendGuardLoader.so /usr/local/zend/
    fi
    echo "Write ZendGuardLoader to php.ini......"
    cat >>/usr/local/php/etc/php.ini<<EOF
;eaccelerator
;ionCube
[Zend Optimizer] 
zend_extension=/usr/local/zend/ZendGuardLoader.so
zend_loader.enable=1
zend_loader.disable_licensing=0
zend_loader.obfuscation_level_support=3
zend_loader.license_path=
;xcache
;xcache end
EOF
    if [ "${Stack}" = "lnmp" ]; then
        echo "Creating new php-fpm configure file......"
        cat >/usr/local/php/etc/php-fpm.conf<<EOF
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
log_level = error
[www]
listen = /dev/shm/php-cgi.sock
;listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 6
request_terminate_timeout = 600
request_slowlog_timeout = 0
slowlog = var/log/slow.log
EOF
        echo "Copy php-fpm init.d file......"
        \cp ${cur_dir}/src/php-${php_version}/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
        chmod +x /etc/init.d/php-fpm
    fi
    lnmp start
    Check_PHP_Upgrade_Files
}

Upgrade_PHP_54()
{
    Echo_Blue "Start install php-${php_version}"
    Tar_Cd php-${php_version}.tar.gz php-${php_version}
    if [ "${Stack}" = "lnmp" ]; then
        ./configure --prefix=/usr/local/php-${php_version} --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo ${PHP_Modules_Arguments}
     else
        ./configure --prefix=/usr/local/php-${php_version} --with-config-file-path=/usr/local/php/etc --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo ${PHP_Modules_Arguments}
        sed -i 's/#LoadModule php5_module/LoadModule php5_module/g' /usr/local/apache/conf/httpd.conf
    fi
    make ZEND_EXTRA_LIBS='-liconv'
    make install
    rm -f /usr/local/php
    ln -sf /usr/local/php-${php_version} /usr/local/php
    Ln_PHP_Bin
    echo "Copy new php configure file."
    mkdir -p /usr/local/php/etc
    \cp php.ini-production /usr/local/php/etc/php.ini
    cd ${cur_dir}
    # php extensions
    echo "Modify php.ini......"
    sed -i 's/post_max_size = 8M/post_max_size = 512M/g' /usr/local/php/etc/php.ini
    sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 500M/g' /usr/local/php/etc/php.ini
    sed -i 's/;date.timezone =/date.timezone = PRC/g' /usr/local/php/etc/php.ini
    sed -i 's/short_open_tag = Off/short_open_tag = On/g' /usr/local/php/etc/php.ini
    sed -i 's/; cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini
    sed -i 's/;cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini
    sed -i 's/max_execution_time = 30/max_execution_time = 300/g' /usr/local/php/etc/php.ini
    sed -i 's/max_input_time = 60/max_input_time = 600/g' /usr/local/php/etc/php.ini
    sed -i 's/memory_limit = 128M/memory_limit = 2048M/g' /usr/local/php/etc/php.ini
    sed -i 's/disable_functions =.*/disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server/g' /usr/local/php/etc/php.ini
    Pear_Pecl_Set
    echo "Install ZendGuardLoader..."
    cd ${cur_dir}/src
    if [ "${Is_64bit}" = "y" ] ; then
        Download_Files ${Download_Mirror}/zend/ZendGuardLoader-70429-PHP-5.4-linux-glibc23-x86_64.tar.gz ZendGuardLoader-70429-PHP-5.4-linux-glibc23-x86_64.tar.gz
        tar zxf ZendGuardLoader-70429-PHP-5.4-linux-glibc23-x86_64.tar.gz
        mkdir -p /usr/local/zend/
        \cp ZendGuardLoader-70429-PHP-5.4-linux-glibc23-x86_64/php-5.4.x/ZendGuardLoader.so /usr/local/zend/
     else
        Download_Files ${Download_Mirror}/zend/ZendGuardLoader-70429-PHP-5.4-linux-glibc23-i386.tar.gz ZendGuardLoader-70429-PHP-5.4-linux-glibc23-i386.tar.gz
        tar zxf ZendGuardLoader-70429-PHP-5.4-linux-glibc23-i386.tar.gz
        mkdir -p /usr/local/zend/
        \cp ZendGuardLoader-70429-PHP-5.4-linux-glibc23-i386/php-5.4.x/ZendGuardLoader.so /usr/local/zend/
    fi
    echo "Write ZendGuardLoader to php.ini......"
    cat >>/usr/local/php/etc/php.ini<<EOF
;eaccelerator
;ionCube
[Zend ZendGuard Loader]
zend_extension=/usr/local/zend/ZendGuardLoader.so
zend_loader.enable=1
zend_loader.disable_licensing=0
zend_loader.obfuscation_level_support=3
zend_loader.license_path=
;xcache
;xcache end
EOF
    if [ "${Stack}" = "lnmp" ]; then
        echo "Creating new php-fpm configure file......"
        cat >/usr/local/php/etc/php-fpm.conf<<EOF
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
log_level = error
[www]
listen = /dev/shm/php-cgi.sock
;listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 6
request_terminate_timeout = 600
request_slowlog_timeout = 0
slowlog = var/log/slow.log
EOF
        echo "Copy php-fpm init.d file......"
        \cp ${cur_dir}/src/php-${php_version}/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
        chmod +x /etc/init.d/php-fpm
    fi
    lnmp start
    Check_PHP_Upgrade_Files
}

Upgrade_PHP_556()
{
    Echo_Blue "Start install php-${php_version}"
    Tar_Cd php-${php_version}.tar.gz php-${php_version}
    if [ "${Stack}" = "lnmp" ]; then
        ./configure --prefix=/usr/local/php-${php_version} --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache ${PHP_Modules_Arguments}
     else
       ./configure --prefix=/usr/local/php-${php_version} --with-config-file-path=/usr/local/php/etc --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache ${PHP_Modules_Arguments}
        sed -i 's/#LoadModule php5_module/LoadModule php5_module/g' /usr/local/apache/conf/httpd.conf
    fi
    make ZEND_EXTRA_LIBS='-liconv'
    make install
    rm -f /usr/local/php
    ln -sf /usr/local/php-${php_version} /usr/local/php
    Ln_PHP_Bin
    echo "Copy new php configure file."
    mkdir -p /usr/local/php/etc
    \cp php.ini-production /usr/local/php/etc/php.ini
    cd ${cur_dir}
    # php extensions
    echo "Modify php.ini......"
    sed -i 's/post_max_size = 8M/post_max_size = 512M/g' /usr/local/php/etc/php.ini
    sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 500M/g' /usr/local/php/etc/php.ini
    sed -i 's/;date.timezone =/date.timezone = PRC/g' /usr/local/php/etc/php.ini
    sed -i 's/short_open_tag = Off/short_open_tag = On/g' /usr/local/php/etc/php.ini
    sed -i 's/; cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini
    sed -i 's/;cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini
    sed -i 's/max_execution_time = 30/max_execution_time = 300/g' /usr/local/php/etc/php.ini
    sed -i 's/max_input_time = 60/max_input_time = 600/g' /usr/local/php/etc/php.ini
    sed -i 's/memory_limit = 128M/memory_limit = 2048M/g' /usr/local/php/etc/php.ini
    sed -i 's/disable_functions =.*/disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server/g' /usr/local/php/etc/php.ini
    Pear_Pecl_Set
    if echo "${php_version}" | grep -Eqi '^5.5.';then
        echo "Install ZendGuardLoader for PHP 5.5..."
        cd ${cur_dir}/src
        if [ "${Is_64bit}" = "y" ] ; then
            Download_Files ${Download_Mirror}/zend/zend-loader-php5.5-linux-x86_64.tar.gz zend-loader-php5.5-linux-x86_64.tar.gz
            tar zxf zend-loader-php5.5-linux-x86_64.tar.gz
            mkdir -p /usr/local/zend/
            \cp zend-loader-php5.5-linux-x86_64/ZendGuardLoader.so /usr/local/zend/
         else
            Download_Files ${Download_Mirror}/zend/zend-loader-php5.5-linux-i386.tar.gz zend-loader-php5.5-linux-i386.tar.gz
            tar zxf zend-loader-php5.5-linux-i386.tar.gz
            mkdir -p /usr/local/zend/
            \cp zend-loader-php5.5-linux-i386/ZendGuardLoader.so /usr/local/zend/
        fi
     elif echo "${php_version}" | grep -Eqi '^5.6.';then
        echo "Install ZendGuardLoader for PHP 5.6..."
        cd ${cur_dir}/src
        if [ "${Is_64bit}" = "y" ] ; then
            Download_Files ${Download_Mirror}/zend/zend-loader-php5.6-linux-x86_64.tar.gz zend-loader-php5.6-linux-x86_64.tar.gz
            tar zxf zend-loader-php5.6-linux-x86_64.tar.gz
            mkdir -p /usr/local/zend/
            \cp zend-loader-php5.6-linux-x86_64/ZendGuardLoader.so /usr/local/zend/
         else
            Download_Files ${Download_Mirror}/zend/zend-loader-php5.6-linux-i386.tar.gz zend-loader-php5.6-linux-i386.tar.gz
            tar zxf zend-loader-php5.6-linux-i386.tar.gz
            mkdir -p /usr/local/zend/
            \cp zend-loader-php5.6-linux-i386/ZendGuardLoader.so /usr/local/zend/
        fi
    fi
    echo "Write ZendGuardLoader to php.ini......"
    cat >>/usr/local/php/etc/php.ini<<EOF
;eaccelerator
;ionCube
[Zend ZendGuard Loader]
zend_extension=/usr/local/zend/ZendGuardLoader.so
zend_loader.enable=1
zend_loader.disable_licensing=0
zend_loader.obfuscation_level_support=3
zend_loader.license_path=
;opcache
[Zend Opcache]
zend_extension=opcache.so
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1
;opcache end
;xcache
;xcache end
EOF
    echo "Download Opcache Control Panel..."
    \cp ${cur_dir}/conf/ocp.php /home/wwwroot/default/ocp.php
    if [ "${Stack}" = "lnmp" ]; then
        echo "Creating new php-fpm configure file......"
        cat >/usr/local/php/etc/php-fpm.conf<<EOF
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
log_level = error
[www]
listen = /dev/shm/php-cgi.sock
;listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 6
request_terminate_timeout = 600
request_slowlog_timeout = 0
slowlog = var/log/slow.log
EOF
        echo "Copy php-fpm init.d file......"
        \cp ${cur_dir}/src/php-${php_version}/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
        chmod +x /etc/init.d/php-fpm
    fi
    lnmp start
    Check_PHP_Upgrade_Files
}

Upgrade_PHP_70()
{
    Echo_Blue "Start install php-${php_version}"
    Tar_Cd php-${php_version}.tar.gz php-${php_version}
    if [ "${Stack}" = "lnmp" ]; then
        ./configure --prefix=/usr/local/php-${php_version} --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-intl --enable-pcntl --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache ${PHP_Modules_Arguments}
     else
        ./configure --prefix=/usr/local/php-${php_version} --with-config-file-path=/usr/local/php/etc --with-apxs2=/usr/local/apache/bin/apxs --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-intl --enable-pcntl --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache ${PHP_Modules_Arguments}
        sed -i 's/LoadModule php5_module/#LoadModule php5_module/g' /usr/local/apache/conf/httpd.conf
        sed -i 's/#LoadModule php7_module/LoadModule php7_module/g' /usr/local/apache/conf/httpd.conf
    fi
    make ZEND_EXTRA_LIBS='-liconv'
    make install
    rm -f /usr/local/php
    ln -sf /usr/local/php-${php_version} /usr/local/php
    Ln_PHP_Bin
    echo "Copy new php configure file."
    mkdir -p /usr/local/php/etc
    \cp php.ini-production /usr/local/php/etc/php.ini
    cd ${cur_dir}
    # php extensions
    echo "Modify php.ini......"
    sed -i 's/post_max_size = 8M/post_max_size = 512M/g' /usr/local/php/etc/php.ini
    sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 500M/g' /usr/local/php/etc/php.ini
    sed -i 's/;date.timezone =/date.timezone = PRC/g' /usr/local/php/etc/php.ini
    sed -i 's/short_open_tag = Off/short_open_tag = On/g' /usr/local/php/etc/php.ini
    sed -i 's/; cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini
    sed -i 's/;cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini
    sed -i 's/max_execution_time = 30/max_execution_time = 300/g' /usr/local/php/etc/php.ini
    sed -i 's/max_input_time = 60/max_input_time = 600/g' /usr/local/php/etc/php.ini
    sed -i 's/memory_limit = 128M/memory_limit = 2048M/g' /usr/local/php/etc/php.ini
    sed -i 's/disable_functions =.*/disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server/g' /usr/local/php/etc/php.ini
    Pear_Pecl_Set
    echo "Install ZendGuardLoader for PHP 7..."
    echo "No ZendGuardLoader for PHP 7"
    echo "Write ZendGuardLoader to php.ini......"
    cat >>/usr/local/php/etc/php.ini<<EOF
;eaccelerator
;ionCube
[Zend ZendGuard Loader]
;zend_extension=/usr/local/zend/ZendGuardLoader.so
;zend_loader.enable=1
;zend_loader.disable_licensing=0
;zend_loader.obfuscation_level_support=3
;zend_loader.license_path=
;opcache
[Zend Opcache]
zend_extension=opcache.so
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1
;opcache end
;xcache
;xcache end
EOF
    echo "Download Opcache Control Panel..."
    \cp ${cur_dir}/conf/ocp.php /home/wwwroot/default/ocp.php
    if [ "${Stack}" = "lnmp" ]; then
        echo "Creating new php-fpm configure file......"
        cat >/usr/local/php/etc/php-fpm.conf<<EOF
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
log_level = error
[www]
listen = /dev/shm/php-cgi.sock
;listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 6
request_terminate_timeout = 600
request_slowlog_timeout = 0
slowlog = var/log/slow.log
EOF
        echo "Copy php-fpm init.d file......"
        \cp ${cur_dir}/src/php-${php_version}/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
        chmod +x /etc/init.d/php-fpm
    fi
    lnmp start
    Check_PHP_Upgrade_Files
}

Upgrade_PHP()
{
    Start_Upgrade_PHP
    if [ -d /usr/local/php-${php_version} ]; then
        if [ "${Stack}" = "lnmp" ]; then
            mv /usr/local/php-${php_version}/php-fpm-${php_version} /etc/init.d/php-fpm
            chmod +x /etc/init.d/php-fpm
         else
            if echo "${php_version}" | grep -Eqi '^7.0.';then
                sed -i 's/LoadModule php5_module/#LoadModule php5_module/g' /usr/local/apache/conf/httpd.conf
                sed -i 's/#LoadModule php7_module/LoadModule php7_module/g' /usr/local/apache/conf/httpd.conf
                mv /usr/local/apache/modules/libphp7.so-${php_version} /usr/local/apache/modules/libphp7.so
             else
                sed -i 's/#LoadModule php5_module/LoadModule php5_module/g' /usr/local/apache/conf/httpd.conf
                sed -i 's/LoadModule php7_module/#LoadModule php7_module/g' /usr/local/apache/conf/httpd.conf
                mv /usr/local/apache/modules/libphp5.so-${php_version} /usr/local/apache/modules/libphp5.so
            fi
        fi
        ln -sf /usr/local/php-${php_version} /usr/local/php
        lnmp start
        Check_PHP_Upgrade_Files
     else
        Check_Libtidy
        if echo "${php_version}" | grep -Eqi '^5.2.';then
            Upgrade_PHP_52
         elif echo "${php_version}" | grep -Eqi '^5.3.';then
            Upgrade_PHP_53
         elif echo "${php_version}" | grep -Eqi '^5.4.';then
            Upgrade_PHP_54
         elif echo "${php_version}" | grep -Eqi '^5.[56].';then
            Upgrade_PHP_556
         elif echo "${php_version}" | grep -Eqi '^7.0.';then
            Upgrade_PHP_70
        fi
        if [ "${Stack}" = "lnmp" ]; then
            LNMP_PHP_Opt
            /etc/init.d/php-fpm restart
        fi
    fi
}
